package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Transactional
@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImp implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //拼接日期的范围
        String begin = date + "-1";
        String end = date + "-31";
        //将日期范围封装到map集合中,调用dao
        HashMap<String, String> map = new HashMap<>();
        map.put("begin", begin);
        map.put("end", end);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        //将返回的List<OrderSetting>转换成List<Map>集合
        List<Map> list1 = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map<String, Object> m = new HashMap<>();
            //根据前台需要的数据格式进行调整
            m.put("date", orderSetting.getOrderDate().getDate());
            m.put("number", orderSetting.getNumber());
            m.put("reservations", orderSetting.getReservations());
            list1.add(m);
        }
        return list1;
    }

    @Override
    public void upload(List<OrderSetting> data) {
        for (OrderSetting orderSetting : data) {
            Date orderDate = orderSetting.getOrderDate();
            Long count = orderSettingDao.findCountByOrderDate(orderDate);
            if (count > 0) {
                //当前日期已经进行了预约设置
                orderSettingDao.editNumberByOrderDate(orderSetting);
            } else {
                //当前日期没有进行预约设置
                orderSettingDao.add(orderSetting);
            }
        }
    }

    //根据日期设置当日可预约数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        Date orderDate = orderSetting.getOrderDate();
        //根据日期判断是否存在
        Long count = orderSettingDao.findCountByOrderDate(orderDate);
        if (count > 0) {
            //当前日期已经进行了预约设置
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            //当前日期没有进行预约设置
            orderSettingDao.add(orderSetting);
        }
    }
}
